
import 'package:amway_superapp_creator_plugin/api.dart';
import 'package:amway_superapp_creator_plugin/models/guide_model.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_flavor/flutter_flavor.dart';
import 'package:uuid/uuid.dart';

class HomeGuideListViewModel extends BaseModel {
  List<GuideModel> _datas = [];

  HomeGuideListViewModel({@required Api api}) : super(api: api);

  void queryGuideDatas(bool isHome) async {
    var queryParameters = {
      "q": "创玩指南",
      "role": "abo",
      "level": "SA_M",
      "start": 0,
      "rows": 10,
      "sort": "published_at"
    };
    var uuid = Uuid();
    // Generate a v1 (time-based) id
    var v1 = uuid.v1();
    var heards = {
      "Authorization": FlavorConfig.instance.variables["AppCode"],
      "X-Ca-Nonce": v1
    };
    var amwayBaseUrl = FlavorConfig.instance.variables["amwayBaseUrl"];
    Options options = Options(headers: heards);
    try{
      Dio dio = Dio();
      Response response = await dio.get(amwayBaseUrl + "/creators/guidelines",
          queryParameters: queryParameters, options: options);
      if (response.statusCode == 200) {
        response.data.forEach((element) {
          datas.add(GuideModel.fromJson(element));
        });
        notifyListeners();
      } else {
        print("失败了");
      }
    }catch(e){
      print(e);


    }

  }

  List get datas => _datas;

  set datas(List<GuideModel> datas) {
    _datas = datas;
    notifyListeners();
  }
}
